package com.ruoyi.web.controller.api.patient;

import com.ruoyi.common.constant.HttpStatusCusCode;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.CustomizeDataInfo;
import com.ruoyi.system.domain.api.patient.*;
import com.ruoyi.system.service.api.patient.IGroupApiService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * 分组Controller
 * 
 * @author sergio
 * @date 2024-11-13
 */
@Api("分组信息")
@RestController
@RequestMapping("/api/patient/group")
public class GroupApiController extends BaseController
{
    @Autowired
    private IGroupApiService groupApiService;

    /**
     * 查询分组列表
     */
    @ApiOperation("查询分组列表")
    @GetMapping("/list")
    public CustomizeDataInfo list(GroupDto group)
    {
        startPage();
        List<GroupDto> list = groupApiService.selectGroupList(group);
        return getCustomizeDataTable(list);
    }

    /**
     * 根据医生ID和医院ID获取分组列表
     */
    @ApiOperation("根据医生ID和医院ID获取分组列表")
    @PostMapping("/listByDocId")
    public CustomizeDataInfo listByDocId(@RequestBody GroupDto group)
    {
        if(group.getHosId() == null || group.getDoctorId() == null){
            // 任意一个为空时的处理逻辑
            CustomizeDataInfo errorResponse = new CustomizeDataInfo();
            errorResponse.setCode(HttpStatusCusCode.DOCTOR_HOS_ID_NOT_FOUND.getCode());
            errorResponse.setMsg(HttpStatusCusCode.DOCTOR_HOS_ID_NOT_FOUND.getMessage());
            return errorResponse;
        }
        startPage();
        // 调用服务获取分组列表
        List<GroupDto> list = groupApiService.selectGroupListByDocId(group);
        return getCustomizeDataTable(list);
    }

    /**
     * 根据分组成员获取分组列表
     */
    @ApiOperation("根据分组成员获取分组列表")
    @PostMapping("/listByUserId")
    public CustomizeDataInfo listByUserId(@RequestBody GroupMember groupMember)
    {
        if(groupMember.getUserId() == null || groupMember.getUserType() == null){
            // 任意一个为空时的处理逻辑
            CustomizeDataInfo errorResponse = new CustomizeDataInfo();
            errorResponse.setCode(HttpStatusCusCode.USER_TYPE_ID_NULL.getCode());
            errorResponse.setMsg(HttpStatusCusCode.USER_TYPE_ID_NULL.getMessage());
            return errorResponse;
        }
        startPage();
        // 调用服务获取分组列表
        List<GroupMemberDto> list = groupApiService.selectGroupListByUserId(groupMember);
        return getCustomizeDataTable(list);
    }

    /**
     * 添加分组成员
     */
    @ApiOperation("添加分组成员")
    @PostMapping("/addGroupMember")
    public CustomizeDataInfo addGroupMember(@RequestBody GroupMemberDto groupMemberDto)
    {
        startPage();
        if(groupMemberDto.getGroupId() == null){
            // 任意一个为空时的处理逻辑
            CustomizeDataInfo errorResponse = new CustomizeDataInfo();
            errorResponse.setCode(HttpStatusCusCode.GROUP_ID_NOT_FOUND.getCode());
            errorResponse.setMsg(HttpStatusCusCode.GROUP_ID_NOT_FOUND.getMessage());
            return errorResponse;
        }
        List<GroupMember> groupMembers = groupMemberDto.getGroupMember();
        for (GroupMember member : groupMembers) {
            if (member.getUserId() == null) {
                // 任意一个为空时的处理逻辑
                CustomizeDataInfo errorResponse = new CustomizeDataInfo();
                errorResponse.setCode(HttpStatusCusCode.USER_ID_NULL.getCode());
                errorResponse.setMsg(HttpStatusCusCode.USER_ID_NULL.getMessage());
                return errorResponse;
            }
            if (member.getUserType() == null) {
                // 任意一个为空时的处理逻辑
                CustomizeDataInfo errorResponse = new CustomizeDataInfo();
                errorResponse.setCode(HttpStatusCusCode.USER_TYPE_NOT_NULL.getCode());
                errorResponse.setMsg(HttpStatusCusCode.USER_TYPE_NOT_NULL.getMessage());
                return errorResponse;
            }

        }
        // 调用服务获取分组列表
        List<GroupMember> list = groupApiService.addGroupMember(groupMemberDto);
        if(list==null){
            // 任意一个为空时的处理逻辑
            CustomizeDataInfo errorResponse = new CustomizeDataInfo();
            errorResponse.setCode(HttpStatusCusCode.GROUP_NOT_FOUNT.getCode());
            errorResponse.setMsg(HttpStatusCusCode.GROUP_NOT_FOUNT.getMessage());
            return errorResponse;
        } else if (list.isEmpty()) {
            // 任意一个为空时的处理逻辑
            CustomizeDataInfo errorResponse = new CustomizeDataInfo();
            errorResponse.setCode(HttpStatusCusCode.MEMBER_IS_EXIST.getCode());
            errorResponse.setMsg(HttpStatusCusCode.MEMBER_IS_EXIST.getMessage());
            return errorResponse;
        }

        return getCustomizeDataTable(list);
    }

    /**
     * 移除分组成员
     *
     */
    @ApiOperation("移除分组成员")
    @PostMapping("/delGroupMember")
    public AjaxResult delGroupMember(@RequestBody GroupMemberDto groupMemberDto)
    {
        startPage();
        if(groupMemberDto.getGroupId() == null){
            return AjaxResult.error(HttpStatusCusCode.GROUP_ID_NOT_FOUND.getCode(),HttpStatusCusCode.GROUP_ID_NOT_FOUND.getMessage());
        }
        if(groupMemberDto.getUserId() == null){
            return AjaxResult.error(HttpStatusCusCode.USER_ID_NULL.getCode(),HttpStatusCusCode.USER_ID_NULL.getMessage());
        }
        if(groupMemberDto.getUserType() == null){
            return AjaxResult.error(HttpStatusCusCode.USER_TYPE_NOT_NULL.getCode(),HttpStatusCusCode.USER_TYPE_NOT_NULL.getMessage());
        }
        // 调用服务获取分组列表
        int status = groupApiService.delGroupMember(groupMemberDto);
        if(status == HttpStatusCusCode.GROUP_MEMBER_NOT_FOUND.getCode()){
            return AjaxResult.error(HttpStatusCusCode.GROUP_MEMBER_NOT_FOUND.getCode(),HttpStatusCusCode.GROUP_MEMBER_NOT_FOUND.getMessage());
        } else if (status == HttpStatusCusCode.GROUP_MEMBER_NO_POWER.getCode()) {
            return AjaxResult.error(HttpStatusCusCode.GROUP_MEMBER_NO_POWER.getCode(),HttpStatusCusCode.GROUP_MEMBER_NO_POWER.getMessage());

        } else if (status == HttpStatusCusCode.USER_MEMBER_NOT_IN_GROUP.getCode()) {
            return AjaxResult.error(HttpStatusCusCode.USER_MEMBER_NOT_IN_GROUP.getCode(),HttpStatusCusCode.USER_MEMBER_NOT_IN_GROUP.getMessage());

        }else if (status == HttpStatusCusCode.DEL_FAILED.getCode()) {
            return AjaxResult.error(HttpStatusCusCode.DEL_FAILED.getCode(),HttpStatusCusCode.DEL_FAILED.getMessage());

        }
        return AjaxResult.success(status);
    }

    /**
     * 退出分组
     *
     */
    @ApiOperation("退出分组")
    @PostMapping("/outGroupMember")
    public AjaxResult outGroupMember(@RequestBody GroupMemberDto groupMemberDto)
    {
        startPage();
        if(groupMemberDto.getGroupId() == null){
            return AjaxResult.error(HttpStatusCusCode.GROUP_ID_NOT_FOUND.getCode(),HttpStatusCusCode.GROUP_ID_NOT_FOUND.getMessage());

        }
        // 调用服务获取分组列表
        int status = groupApiService.outGroupMember(groupMemberDto);
        if(status == HttpStatusCusCode.GROUP_MEMBER_NOT_FOUND.getCode()){
            return AjaxResult.error(HttpStatusCusCode.GROUP_MEMBER_NOT_FOUND.getCode(),HttpStatusCusCode.GROUP_MEMBER_NOT_FOUND.getMessage());
        }
        return AjaxResult.success(status);
    }

    /**
     * 解散该分组
     *
     * @author sergio
     * @date 2024/11/21
     */
    @ApiOperation("解散该分组")
    @PostMapping("/disbandGroup")
    public AjaxResult disbandGroup(@RequestBody GroupDto groupDto)
    {
        if(groupDto.getId() == null){
            return AjaxResult.error(HttpStatusCusCode.ID_NOT_FOUND.getCode(),HttpStatusCusCode.ID_NOT_FOUND.getMessage());
        }
        int status = groupApiService.disbandGroup(groupDto);
        return AjaxResult.success(status);

    }


    /**
     * 获取分组详细信息
     */
    @ApiOperation("获取分组详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(groupApiService.selectGroupById(id));
    }

    /**
     * 新增分组
     */
    @ApiOperation("新增分组")
    @PostMapping("/addGroup")
    public AjaxResult addGroup(@RequestBody GroupDto group)
    {
        if (group.getHosId() == null || group.getDoctorId() == null || group.getGroupName() == null || group.getGroupName().isEmpty()) {
            return AjaxResult.error(HttpStatusCusCode.GROUP_NAME_NOT_FOUNT.getCode(),HttpStatusCusCode.GROUP_NAME_NOT_FOUNT.getMessage());
        } else if (group.getGroupMember().isEmpty()) {
            // 新成员不得为空 至少两人才能成为一个分组
            return AjaxResult.error(HttpStatusCusCode.GROUP_MEMBER_NULL.getCode(),HttpStatusCusCode.GROUP_MEMBER_NULL.getMessage());
        }
        List<GroupMember> groupMembers = group.getGroupMember();
        // 判断user_id是否为空
        for (GroupMember member : groupMembers) {
            if (member.getUserId() == null) {
                return AjaxResult.error(HttpStatusCusCode.USER_ID_NULL.getCode(),HttpStatusCusCode.USER_ID_NULL.getMessage());
            }
        }
        return AjaxResult.success(groupApiService.insertGroup(group));
    }

    /**
     * 查看分组详情及分组成员
     *
     * @author sergio
     * @date 2024/11/18
     */
    @ApiOperation("查看分组详情及分组成员")
    @PostMapping("/groupDetail")
    public AjaxResult groupDetail(@RequestBody GroupMember groupMember)
    {
        if (groupMember.getGroupId() == null) {
            return AjaxResult.error(HttpStatusCusCode.GROUP_ID_NOT_FOUND.getCode(),HttpStatusCusCode.GROUP_ID_NOT_FOUND.getMessage());
        }
        GroupMemberDto groupMemberDto = groupApiService.selectGroupDetailByGroupId(groupMember.getGroupId());
        return AjaxResult.success(groupMemberDto);
    }

    /**
     * 添加分组成员中-获取已添加及未添加的绑定的患者列表
     *
     * @author sergio
     * @date 2024/11/14
     */
    @ApiOperation("添加分组成员中-获取已添加及未添加的绑定的患者列表")
    @PostMapping("/addMemberList")
    public CustomizeDataInfo addMemberList(@RequestBody @Valid GroupMember groupMember)
    {
        if (groupMember.getGroupId() == null) {
            CustomizeDataInfo errorResponse = new CustomizeDataInfo();
            errorResponse.setCode(HttpStatusCusCode.GROUP_ID_NOT_FOUND.getCode());
            errorResponse.setMsg(HttpStatusCusCode.GROUP_ID_NOT_FOUND.getMessage());
            return errorResponse;
        }
        // 两者都不为空时的处理逻辑
        startPage();
        List<PatientInfoDto> patientInfoDtoList = groupApiService.getPatientListByGroup(groupMember.getGroupId());
        return getCustomizeDataTable(patientInfoDtoList);
    }

    /**
     * 修改分组
     */
    @ApiOperation("修改分组")
    @PutMapping
    public AjaxResult edit(@RequestBody GroupDto group)
    {
        return toAjax(groupApiService.updateGroup(group));
    }

    /**
     * 删除分组
     */
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(groupApiService.deleteGroupByIds(ids));
    }
}
